package com.entrogy.controller;

import com.entrogy.service.ClassService;
import com.entrogy.service.KaoqinService;
import com.entrogy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName KaoqinController
 * @Description
 * @Author luoxingjiang
 * @Date 2019/7/5 0005 上午 9:11
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/kaoqin")
@Api(description = "三原旧接口，宿舍考勤和平安考勤（学生考勤）均已重写")
public class KaoqinController {

    private Logger logger = LoggerFactory.getLogger(KaoqinController.class);

    @Autowired
    private KaoqinService kaoqinService;

    @Autowired
    private UserService userService;

    @Autowired
    private ClassService classService;

    // 页面初始化
    @RequestMapping(value = "/kaoqinInit", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query"),
    })
    @ApiOperation(value = "返回平安考勤页面的基本信息")
    public JSONObject kaoqinInit(String uid) {
        JSONObject returnObj = new JSONObject();
        logger.info("kaoqinInit:" + uid);
        try {
            returnObj = kaoqinService.kaoqinInit(uid);
            if (returnObj == null || !returnObj.containsKey("list1")) {
                JSONObject tmp = new JSONObject();
                tmp.put("errcode", 1);
                tmp.put("errmsg", "没有权限");
                return tmp;
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObj.put("errcode", 1);
            returnObj.put("errmsg", "考勤页面初始化失败");
            logger.error("kaoqinInit:" + uid);
            return returnObj;
        }
        returnObj.put("errcode", 0);
        returnObj.put("errmsg", "ok");
        return returnObj;
    }

    // 筛选
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "selectid", value = "要查看的班级id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "timeid", value = "时间段id", dataType = "String", paramType = "query"),
    })
    @ApiOperation(value = "选择查看的考勤时间段")
    public JSONObject selectTime(String selectid, String uid, String timeid) {
//        JSONObject returnObj = new JSONObject();
//        JSONArray returnArray;
//        Boolean isLifeTeacher;
//        Boolean isConstabulary;
//        logger.info("select:" + uid);
//        try {
//            returnArray = kaoqinService.select(uid, selectid, timeid);
//            //判断用户是否是生活老师
//            isLifeTeacher = userService.isLifeTeacher(uid);
//            //判断是否是保安队的
//            isConstabulary = userService.isConstabulary(uid);
//        } catch (Exception e) {
//            returnObj.put("errcode", 1);
//            returnObj.put("errmsg", "查询失败");
//            logger.error("select:" + selectid + " " + uid + " " + timeid);
//            e.printStackTrace();
//            return returnObj;
//        }
//        if (isLifeTeacher == true) {
//            returnObj.put("title", "宿舍考勤");
//            returnObj.put("list4", kaoqinService.colTitle(1));
//        } else if (isConstabulary == true) {
//            returnObj.put("title", "请假查看");
//            returnObj.put("list4", kaoqinService.colTitle(1));
//        } else {
//            returnObj.put("title", "平安考勤");
//            returnObj.put("list4", kaoqinService.colTitle(2));
//        }
//        returnObj.put("errcode", 0);
//        returnObj.put("errmsg", "ok");
//        returnObj.put("list3", returnArray);
        return null;
    }

    // 详细
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", value = "班级id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query"),
    })
    @ApiOperation(value = "查看班级早、中、晚考勤详情")
    public JSONObject detail(String courseId, String uid) {
        JSONObject returnObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        String name = "";
        try {
            Integer schoolCode = userService.getUserByUid(uid).getSchoolCode();
            jsonArray = kaoqinService.detailRecordByTea(courseId, schoolCode);
            name = classService.getClassById(Integer.valueOf(courseId), schoolCode).getClassName();
        } catch (Exception e) {
            returnObj.put("errcode", 1);
            returnObj.put("errmsg", "查询详情失败");
            logger.error("detail:" + courseId);
            e.printStackTrace();
        }
        returnObj.put("errcode", 0);
        returnObj.put("errmsg", "ok");
        returnObj.put("title", "平安考勤");
        returnObj.put("classname", name);
        returnObj.put("detail", jsonArray);
        return returnObj;
    }

    // 宿舍页面初始化
    @RequestMapping(value = "/susheInit", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "String", paramType = "query"),
    })
    @ApiOperation(value = "查看宿舍考勤详情")
    public JSONObject susheInit(String uid){
//        JSONObject returnObj = new JSONObject();
//        logger.info("susheInit:" + uid);
//        try {
//            boolean isLifeTeacher = userService.isLifeTeacher(uid);
//            //是否是德育处职员
//            boolean isDeyuchu = userService.isDeyuchu(uid);
//            if (isLifeTeacher != true && isDeyuchu != true) {
//                returnObj.put("errcode", 1);
//                returnObj.put("errmsg", "没有权限");
//                return returnObj;
//            }
//            returnObj = kaoqinService.susheInit(uid);
//        } catch (Exception e) {
//            returnObj.put("errcode", 1);
//            returnObj.put("errmsg", "宿舍页面初始化失败");
//            logger.error("susheInit:" + uid);
//            e.printStackTrace();
//            return returnObj;
//        }
//        returnObj.put("errcode", 0);
//        returnObj.put("errmsg", "ok");
        return null;
    }



}
